package UI.LessonPlanViewer
{
	import Events.*;
	
	import LessonPlans.*;
	import LessonPlans.Tasks.*;
	
	import UI.LessonPlan.*;
	import UI.Scrollbar.*;
	
	import flash.display.*;
	
	import mx.core.UIComponent;
	
	public class UILessonPlanViewer extends UIComponent
	{
		private var toolbar:UILessonPlanViewerToolbar;
		private var lessonPlan:UILessonPlan;
		private var scrollbar:UIScrollbar;
		
		private var background:Bitmap;
		private var scrollMask:Bitmap;
		
		public function UILessonPlanViewer(toolbarAssets:Object)
		{
			super();
			toolbar = new UILessonPlanViewerToolbar(toolbarAssets);
			this.lessonPlan = new UILessonPlan(new AbstractLessonPlan("","",new Date()));
			scrollbar=new UIScrollbar();
			background=new Bitmap(new BitmapData(toolbar.width,toolbar.height+lessonPlan.height));
			scrollMask=new Bitmap(new BitmapData(1,1));
			
			addEventListeners();
			positionElements();
			addChildren();
		}
		
		private function addEventListeners():void
		{
			toolbar.addEventListener(UIToolbarEvent.ADD_TASK, dispatchToolbarEvent);
			toolbar.addEventListener(UIToolbarEvent.OPEN, dispatchToolbarEvent);
			toolbar.addEventListener(UIToolbarEvent.CLEAR, dispatchToolbarEvent);
			toolbar.addEventListener(UIToolbarEvent.SAVE, dispatchToolbarEvent);
			toolbar.addEventListener(UIToolbarEvent.HISTORY, dispatchToolbarEvent);
			lessonPlan.addEventListener(UIInputBoxEvent.EXPAND,function(e:UIInputBoxEvent):void{positionElements();});
			scrollbar.addEventListener(UIScrollbarEvent.SCROLL,scroll);
		}
		
		public function scroll(e:UIScrollbarEvent):void
		{
			var ratio:Number = scrollbar.trackPositionToScrollAreaRatio;
			lessonPlan.y = toolbar.height-(lessonPlan.height*ratio);
		}
		
		public function get lessonPlanData():ILessonPlan
		{
			return lessonPlan.lessonPlanData;
		}
		
		public function updateLessonPlan(lessonPlan:ILessonPlan):void
		{
			removeChild(this.lessonPlan);
			this.lessonPlan = new UILessonPlan(lessonPlan);
			addChild(this.lessonPlan);
			positionElements();
		}
		
		public function addTaskToLessonPlan(task:Task):void
		{
			lessonPlan.addTask(task);
			positionElements();
		}
		
		public function clearLessonPlan():void
		{
			lessonPlan.clear();
			positionElements();
		}
		
		private function dispatchToolbarEvent(e:UIToolbarEvent):void
		{
			dispatchEvent(new UIToolbarEvent(e.type,e.bubbles,e.cancelable));
		}

		private function positionElements():void
		{
			scrollbar.y=toolbar.height;
			scrollbar.x=background.width-scrollbar.width;
			scrollbar.height = height-toolbar.height;
			scrollbar.trackScale=scrollbar.height/lessonPlan.height;
			toolbar.x = background.width/2-toolbar.width/2;
			lessonPlan.y = toolbar.height;
			lessonPlan.width = scrollbar.x;
			lessonPlan.mask = scrollMask;
			scrollMask.width = scrollbar.x;
			scrollMask.height = background.height-toolbar.height;
			scrollMask.y=toolbar.height;
		}
		
		private function addChildren():void
		{
			addChild(background);
			addChild(toolbar);
			addChild(lessonPlan);
			addChild(scrollbar);
			addChild(scrollMask);
		}
		
		public function get toolbarHeight():Number
		{
			return toolbar.height;
		}
		
		public override function set height(h:Number):void
		{
			background.height=h;
			positionElements();
		}
		
		public override function get height():Number
		{
			return background.height;
		}
		
		public override function set width(w:Number):void
		{
			background.width=w;
			positionElements();
		}
		
		public override function get width():Number
		{
			return background.width;
		}
	}
}